package com.tongji.housediy.view
{
	import com.tongji.housediy.events.commonEvent.GridContainerEvent;
	import com.tongji.housediy.events.commonEvent.MoveCamera3D;
	import com.tongji.housediy.events.commonEvent.ShowComplementInforEvent;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.GoodsModel;
	import com.tongji.housediy.model.ViewControlModel;
	import com.tongji.housediy.utils.ConstVariableInventory;
	import com.tongji.housediy.utils.LoadAdmin;
	import com.tongji.housediy.utils.ShowCustomToolTip;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.Furniture;
	import com.tongji.housediy.view.components.GridContainer;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.view.components.InteriorWall;
	import com.tongji.housediy.view.components.NormalDoor;
	import com.tongji.housediy.view.components.NormalWindow;
	import com.tongji.housediy.view.components.OpeningWall;
	import com.tongji.housediy.view.components.RoomBorder;
	import com.tongji.housediy.view.components.RoomFloor;
	import com.tongji.housediy.vo.Goods;
	
	import flash.display.BitmapData;
	import flash.display.GraphicsBitmapFill;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class ShortCutBtnMediator extends Mediator
	{
		[Inject]
		public var view:ShortCutBtnComp;
		
		[Inject]
		public var viewControlModel:ViewControlModel;
		
		[Inject]
		public var stateModel:CompEnableStateModel;
		
		//caddy
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		private var tempValue:Number;
		
		public var roomsXML:XML = 
			<rooms time="" name="">			  
			</rooms>
			
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.incrementButton, "enabled", stateModel, "isViewPointBtnEnable");
			BindingUtils.bindProperty(view.decrementButton, "enabled", stateModel, "isViewPointBtnEnable");
			BindingUtils.bindProperty(view.hScrollBar, "enabled", stateModel, "isViewPointBtnEnable");
			
			this.addContextListener(ShowComplementInforEvent.SHOW_COMPLEMENT_INFOR, showToolTip, ShowComplementInforEvent);
			view.hScrollBar.minimum = ConstVariableInventory.SCROLLBAR_MIN_VALUE;
			view.hScrollBar.maximum = ConstVariableInventory.SCROLLBAR_MAX_VALUE;
			view.hScrollBar.value = ConstVariableInventory.SCROLLBAR_INITIAL_VALUE;
			
			view.incrementButton.addEventListener(MouseEvent.MOUSE_DOWN, decBtnMouseDownHandler);
			view.decrementButton.addEventListener(MouseEvent.MOUSE_DOWN, incBtnMouseDownHandler);
			
			//保存房型
			view.saveButton.addEventListener(MouseEvent.MOUSE_DOWN, saveBtnMouseDownHandler);
			view.openButton.addEventListener(MouseEvent.MOUSE_DOWN, openBtnMouseDownHandler);
			this.addContextListener(GridContainerEvent.RETRIEVE_GRID_CONTAINER, retrieveGridContainerHandler, GridContainerEvent);
		}
		
		private function showToolTip(event:ShowComplementInforEvent):void
		{
			var showToolTip:ShowCustomToolTip = new ShowCustomToolTip(view.saveButton, "保存文件");
		}
		
		private function retrieveGridContainerHandler(event:GridContainerEvent):void
		{
			view.hScrollBar.value = ConstVariableInventory.SCROLLBAR_INITIAL_VALUE;
		}
		
		private function decBtnMouseDownHandler(event:MouseEvent):void
		{
			if (view.hScrollBar.value < ConstVariableInventory.SCROLLBAR_MAX_VALUE)
			{
				view.hScrollBar.value++;
				viewControlModel.dimensionValue += 1;
				//viewControlModel.tempDimensionValue = viewControlModel.dimensionValue - 1;
				if (viewControlModel.dimensionIndex == 0)
					dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_GRID_SCALE, 1, false));
				else
					dispatch(new MoveCamera3D(MoveCamera3D.CAMERA_NEAR));
			}
		}
		
		private function incBtnMouseDownHandler(event:MouseEvent):void
		{
			
			if (view.hScrollBar.value > ConstVariableInventory.SCROLLBAR_MIN_VALUE)
			{
				view.hScrollBar.value--;
				viewControlModel.dimensionValue -= 1;
				//viewControlModel.tempDimensionValue = viewControlModel.dimensionValue + 1;
				if (viewControlModel.dimensionIndex == 0)
					dispatch(new GridContainerEvent(GridContainerEvent.UPDATE_GRID_SCALE, -1, false));
				else
					dispatch(new MoveCamera3D(MoveCamera3D.CAMERA_FAR));
			}
		}
		
		
		//caddy
		private function saveBtnMouseDownHandler(event:MouseEvent):void
		{
		    var roomsXML:XML = 
				<rooms time="" name="">
				</rooms>
			// 添加房间
			for(var i:int = 0; i < roomModel.gridContainer.numChildren; i++)
			{				  
				if(roomModel.gridContainer.getChildAt(i) is AbstractDrawingRoom)
				{
					var room:AbstractDrawingRoom = roomModel.gridContainer.getChildAt(i) as AbstractDrawingRoom;
					var roomXML:XML = 
						<room name="" x="" y="" vertices="" curveVector="" borderCount="" borderVertextList="" roomType="">								
						</room>						
			        roomXML.@name = room.roomName;
					roomXML.@x = room.x;
					roomXML.@y = room.y;
					roomXML.@height = room.roomHeight;
					roomXML.@vertices = room.vertices;
					roomXML.@borderCount = room.borderLength;
					roomXML.@curveVector = room.curveVector;
					roomXML.@borderVertextList = room.borderVertexList;
					roomXML.@roomType = "room";
					roomXML.@curveVector = room.curveVector;
					roomsXML.appendChild(roomXML);
					
					for(var a:int = 0; a < room.numChildren; a++)
					{	
						// 添加地板													
						if(room.getChildAt(a) is RoomFloor)
						{
							var roomFloor:RoomFloor = room.getChildAt(a) as RoomFloor;
							var floorXML:XML =
								<floor x="" y="" vertices="" borderCount="" goodsID="" modelID="" colorID="">
								</floor>							
							floorXML.@x = roomFloor.x;
							floorXML.@y = roomFloor.y;
							//roomsXML.room[i].floor[a].@vertices = roomFloor.;
							floorXML.@borderCount = roomFloor.borderLength;
							floorXML.@goodsID = roomFloor.hostGoods.goodsID;
							floorXML.@modelID = roomFloor.selectedSizeID;
							floorXML.@colorID = roomFloor.selectedColorID;
							
							roomsXML.room[i].appendChild(floorXML);
							
							for(var b:int = 0; b < roomFloor.numChildren; b++)
							{																						
								// 添加家具
								if(roomFloor.getChildAt(b) is Furniture)
								{									
									var furniture:Furniture = roomFloor.getChildAt(b) as Furniture;
									var furnitureXML:XML =
										<furniture x="" y="" goodsID="" modelID="" colorID="">
										</furniture>
									
									furnitureXML.@x = furniture.x;
									furnitureXML.@y = furniture.y;
									furnitureXML.@goodsID = furniture.hostGoods.goodsID;
									furnitureXML.@modelID = furniture.selectedSizeID;
									furnitureXML.@colorID = furniture.selectedColorID;
									
									roomsXML.room[i].floor[a].appendChild(furnitureXML);
								}
								// 添加自定义地板
								else if(roomFloor.getChildAt(b) is IndependentFloor)
								{
									var independentFloor:IndependentFloor = roomFloor.getChildAt(b) as IndependentFloor;
									var independentFloorXML:XML =
										<independentFloor x="" y="" vertices="" borderCount="" goodsID="" sizeID="" colorID="">
										</independentFloor>
										
									independentFloorXML.@x = independentFloor.x;
									independentFloorXML.@y = independentFloor.y;
									independentFloorXML.@vertices = independentFloor.vertices;
									independentFloorXML.@borderCount = independentFloor.borderLength;
									independentFloorXML.@goodsID = independentFloor.roomFloor.hostGoods.goodsID;
									independentFloorXML.@sizeID = independentFloor.roomFloor.selectedSizeID;
									independentFloorXML.@colorID = independentFloor.roomFloor.selectedColorID;
									
									roomsXML.room[i].floor[a].appendChild(independentFloorXML);
								}
								// 添加内墙
								else if(roomFloor.getChildAt(b) is InteriorWall)
								{
									var interiorWall:InteriorWall = roomFloor.getChildAt(b) as InteriorWall;
									var interiorWallXML:XML =
										<interiorWall x="" y="" vertices="" goodsID="" sizeID="" colorID="" skirtingGoodsID="" skirtingSizeID="" skirtingcolorID="">
										</interiorWall>
										
									interiorWallXML.@x = interiorWall.x;
									interiorWallXML.@y = interiorWall.y;
									interiorWallXML.@vertices = interiorWall.vertices;
									
									roomsXML.room[i].floor[a].appendChild(interiorWallXML);
								}
							}
						}						
						else if(room.getChildAt(a) is RoomBorder)
						{							
							var roomBorderCount:int;					
							var xml:XML = 
								<roomBorder index="" x="" y="" vertices="" goodsID="" sizeID="" colorID="" roomType="" >                                  
								</roomBorder>
							
							var roomBorder:RoomBorder = room.getChildAt(a) as RoomBorder;
							xml.@vertices = roomBorder.data;
							xml.@x = roomBorder.x;
							xml.@y = roomBorder.y;
							xml.@goodsID = roomBorder.hostGoods.goodsID;
							xml.@colorID = roomBorder.selectedColorID;
							xml.@sizeID = roomBorder.selectedSizeID;
							xml.@roomType = "room";
							roomsXML.room[i].appendChild(xml);
							//var borderIndex:int = roomsXML.room[i].child("roomBorder").length();
							var borderIndex:int = roomsXML.room[i].roomBorder.length();

							// 添加墙体上的孩子，门、窗、开放墙
							for(var l:int = 0; l < roomBorder.numChildren; l++)
							{
								// 添加墙上的门
								if(roomBorder.getChildAt(l) is NormalDoor)
								{
									var door:NormalDoor = roomBorder.getChildAt(l) as NormalDoor;
									var doorXML:XML =
										<door x="" y="" goodsID="" sizeID="" colorID="">
										</door>
										
									doorXML.@x = door.x;
									doorXML.@y = door.y;
									doorXML.@goodsID = door.hostGoods.goodsID;
									doorXML.@sizeID = door.selectedSizeID;
									doorXML.@colorID = door.selectedColorID;

									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(doorXML);
								}								
								// 添加墙上的窗
								else if(roomBorder.getChildAt(l) is NormalWindow)
								{
									var window:NormalWindow = roomBorder.getChildAt(l) as NormalWindow;
									var windowXML:XML =
										<window x="" y="" goodsID="" sizeID="" colorID="">
										</window>
										
									windowXML.@x = window.x;
									windowXML.@y = window.y;
									windowXML.@goodsID = window.hostGoods.goodsID;
									windowXML.@sizeID = window.selectedSizeID;
									windowXML.@colorID = window.selectedColorID;
									
									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(windowXML);
								}								
								// 添加墙上的开放墙
								else if(roomBorder.getChildAt(l) is OpeningWall)
								{				
									var openingWall:OpeningWall = roomBorder.getChildAt(l) as OpeningWall;
									var openingWallXML:XML =
										<openingWall x="" y=""  vertices="">
										</openingWall>
									
									openingWallXML.@x = openingWall.x;
									openingWallXML.@y = openingWall.y;
									//openingWallXML.@vertices = openingWall.
									roomsXML.room[i].roomBorder[borderIndex-1].appendChild(openingWallXML);	
								}
							}
						}	
					}							
				}
			}
			roomModel.roomXML = roomsXML;
			/*trace(roomsXML.toString());*/
		}
		
		private function openBtnMouseDownHandler(event:MouseEvent):void
		{						
			for(var i:int = 0; i < roomModel.roomXML.room.length(); i++)
			{
				var tempString:String = roomModel.roomXML.room[i].@vertices.toString();				
				var arr:Array = tempString.split(",", tempString.length) as Array;
				
				var room:AbstractDrawingRoom = new AbstractDrawingRoom();
				room._vertices = new Vector.<Number>();
				
				for(var a:int = 0; a < arr.length; a++)
				{
					room._vertices.push(arr[a]);
				}			
				room.x = roomModel.roomXML.room[i].@x;
				room.y = roomModel.roomXML.room[i].@y;
				room.borderLength = roomModel.roomXML.room[i].@borderCount.toString();
				room.height = roomModel.roomXML.room[i].@height;				
				room.roomType = "room";
				room.curveVector = new Vector.<int>();
				
				var tempString2:String = roomModel.roomXML.room[i].@curveVector.toString();				
				var arr2:Array = tempString2.split(",", tempString.length) as Array;
				if (tempString2 == "")
					arr2 = new Array();
				for(var j:int = 0; j < arr2.length; j++)
				{
					room.curveVector.push(arr2[j]);
				}

				for(var l:int = 0; l <roomModel.roomXML.room[i].floor.length(); l++)
				{
					var curveVector:Vector.<int> = new Vector.<int>();
					var borderCount:int;
					var roomType:String = "room";
					var goods:Goods = new Goods();
					for(var b:int = 0; b < goodsModel.floorArrayCollection.length; b++)
					{
						if((goodsModel.floorArrayCollection.getItemAt(b) as Goods).goodsID == roomModel.roomXML.room[i].floor[l].@goodsID.toString())
						{
							goods = goodsModel.floorArrayCollection.getItemAt(b) as Goods;
							goods.selectedColorID = roomModel.roomXML.room[i].floor[l].@colorID.toString();
							goods.selectedSizeID = roomModel.roomXML.room[i].floor[l].@sizeID.toString();							
						}						
					}
					var loader:Loader = LoadAdmin.loadBitmapData(goods.dragPicSrc);
					loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFloorCompleteHandler);
					function loadFloorCompleteHandler(event:Event):void
					{
						event.target.removeEventListener(Event.COMPLETE, loadFloorCompleteHandler);
						// 此处不需要赋值
						//ConstVariableInventory.roomBitmapData = event.target.content.bitmapData;
						
						room.roomFloor.materialBitmapData = event.target.content.bitmapData;
						room.roomFloor.stroke = new GraphicsBitmapFill(room.roomFloor.materialBitmapData);
						
						room.roomFloor.hostGoods = goods;
						room.roomFloor.selectedColorID = goods.selectedColorID;
						room.roomFloor.selectedSizeID = goods.selectedSizeID;
						
						room.roomFloor.reDraw(room.roomFloor.borderLength);
					}
					
					borderCount = roomModel.roomXML.room[i].floor[l].@borderCount.toString();
				}
				/*var mapData:BitmapData;
				if (room.roomType == "room")
				{
					mapData = ConstVariableInventory.roomBitmapData;
				}
				else if (room.roomType == "independentFloor")
				{
					mapData = ConstVariableInventory.floorBitmapData;
				}*/
				
				/*room.roomFloor.materialBitmapData = mapData;
				room.roomFloor.stroke = new GraphicsBitmapFill(mapData);
				room.roomFloor.hostGoods = goods;
				room.roomFloor.selectedColorID = goods.selectedColorID;
				room.roomFloor.selectedSizeID = goods.selectedSizeID;
                
				room.roomFloor.reDraw(room.roomFloor.borderLength);*/
				for (var num:int = 0; num < room.numChildren; num++)
				{
					if (room.getChildAt(num) is RoomBorder)
					{
						
						for(b = 0; b < goodsModel.wallArrayCollection.length; b++)
						{
							if((goodsModel.wallArrayCollection.getItemAt(b) as Goods).goodsID == roomModel.roomXML.room[i].roomBorder[c].@goodsID.toString())
							{
								goods =	goodsModel.wallArrayCollection.getItemAt(b) as Goods;
								goods.selectedColorID = roomModel.roomXML.room[i].roomBorder[c].@colorID.toString();
								goods.selectedSizeID = roomModel.roomXML.room[i].roomBorder[c].@sizeID.toString();	
							}						
						}
						(room.getChildAt(num) as RoomBorder).hostGoods = goods;
					}
				}
				for(var k:int = 0; k < roomModel.roomXML.room[i].roomBorder.length(); k++)
				{
					var tempVertices:String = roomModel.roomXML.room[i].roomBorder[k].@vertices.toString();
					var vertices:Vector.<Number> = new Vector.<Number>();
					var verticesArr:Array = tempVertices.split(",", tempVertices.length) as Array;
					var roomType2:String = roomModel.roomXML.room[i].roomBorder[k].@roomType;
					for(var d:int = 0; d < verticesArr.length; d++)
					{
						vertices.push(verticesArr[d]);
					}	
					var goods2:Goods = new Goods();
					for(var c:int = 0; c < goodsModel.wallArrayCollection.length; c++)
					{
						if((goodsModel.wallArrayCollection.getItemAt(c) as Goods).goodsID == roomModel.roomXML.room[i].roomBorder[k].@goodsID.toString())
						{
						     goods2 = goodsModel.wallArrayCollection.getItemAt(c) as Goods;
							 goods2.selectedColorID = roomModel.roomXML.room[i].roomBorder[k].@colorID.toString();
							 goods2.selectedSizeID = roomModel.roomXML.room[i].roomBorder[k].@sizeID.toString();
						}
					}
					var loader2:Loader = LoadAdmin.loadBitmapData(goods.dragPicSrc);
					loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, loadFloorCompleteHandler2);
					function loadFloorCompleteHandler2(event:Event):void
					{
						event.target.removeEventListener(Event.COMPLETE, loadFloorCompleteHandler);
						ConstVariableInventory.roomBitmapData = event.target.content.bitmapData;
						
						room.roomFloor.materialBitmapData = event.target.content.bitmapData;
						room.roomFloor.stroke = new GraphicsBitmapFill(room.roomFloor.materialBitmapData);
						
						room.roomFloor.hostGoods = goods;
						room.roomFloor.selectedColorID = goods.selectedColorID;
						room.roomFloor.selectedSizeID = goods.selectedSizeID;
						
						room.roomFloor.reDraw(room.roomFloor.borderLength);
					}
					var roomBorder:RoomBorder = new RoomBorder(vertices, -1, roomType2);
					roomBorder.x = roomModel.roomXML.room[i].roomBorder[k].@x;
					roomBorder.y = roomModel.roomXML.room[i].roomBorder[k].@y;
					roomBorder.selectedColorID = goods2.selectedColorID;
					roomBorder.selectedSizeID = goods2.selectedSizeID;
					roomBorder.hostGoods = goods2;
					//room.addChild(roomBorder);
				}
				room.drawing();
				roomModel.gridContainer.addChild(room);
				room.parentGridContainer = roomModel.gridContainer;
			}
			
			/*trace(roomModel.roomXML.room[i].floor.length());
			trace(roomModel.roomXML.room[i].roomBorder.length());*/
			
			
			/*trace(roomModel.gridContainer.numChildren);*/
		}
	} 
}